﻿using Media.Hepler;
using Media.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;

namespace Media.Controllers
{
    [MediaFilter]
    public class TN_CMSMediaController : ApiController
    {
        ImageRepo _repo = new ImageRepo();

        //[HttpPost]
        //public async Task<HttpResponseMessage> PostImage(string folder)
        //{
        //    // Check if the request contains multipart/form-data.
        //    if (!Request.Content.IsMimeMultipartContent())
        //    {
        //        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        //    }
        //    int maxSize = 0;

        //    //check maxsize
        //    Int32.TryParse(TN_CMSMediaConfig.GetSettingByKey("Image.LengthKB"), out maxSize);
        //    if (Request.Content.Headers.ContentLength / 1024 > maxSize)
        //    {
        //        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        //    }

        //    string root = HttpContext.Current.Server.MapPath("~/App_Data");
        //    var provider = new MultipartFormDataStreamProvider(root);

        //    try
        //    {
        //        // Read the form data.
        //        await Request.Content.ReadAsMultipartAsync(provider);

        //        // This illustrates how to get the file names.
        //        foreach (MultipartFileData file in provider.FileData)
        //        {
        //            //convert <"filename.jpg"> to <filename.jpg>
        //            string fileName = file.Headers.ContentDisposition.FileName.Substring(1, file.Headers.ContentDisposition.FileName.Length - 2);

        //            if (_repo.SaveImage(folder, fileName, file.LocalFileName))
        //            {
        //                return Request.CreateResponse(HttpStatusCode.OK);
        //            }
        //            else
        //            {
        //                return Request.CreateResponse(HttpStatusCode.Forbidden);
        //            }
        //        }

        //        return Request.CreateResponse(HttpStatusCode.OK);
        //    }
        //    catch (System.Exception e)
        //    {
        //        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        //    }
        //}

        [HttpPost]
        public HttpResponseMessage Test(string folder)
        {
            return Request.CreateResponse(HttpStatusCode.OK, new { Folder=folder});
        }

        [HttpGet]
        public HttpResponseMessage Image(string folder, string fileName)
        {
            //proccess request
            string fileType = null;
            byte[] imgData = _repo.GetImage(folder, fileName, out fileType);
            MemoryStream ms = new MemoryStream(imgData);
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StreamContent(ms);
            response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/" + fileType);
            return response;
        }

        public int GetInt()
        {
            return 1;
        }
    }
}
